package com.itheima.d6_synchronized_lock;

public class Ticket implements Runnable {
    private static int ticket = 100;
    private static int sum = 0;

    @Override
    public void run() {
        while (true) {
            if(sellTicket1()){
                break;
            }
        }
    }

    public synchronized  boolean sellTicket1(){
        if (ticket <= 0) {
            return true;
        } else {
            ticket--;
            System.out.println(Thread.currentThread().getName() + "在卖票，还剩 " + ticket + " 张" + "\t总卖：" + ++sum);
            return false;
        }
    }

    public static synchronized  boolean sellTicket2(){
        if (ticket <= 0) {
            return true;
        } else {
            ticket--;
            System.out.println(Thread.currentThread().getName() + "在卖票，还剩 " + ticket + " 张" + "\t总卖：" + ++sum);
            return false;
        }
    }

}
